package com.www.applyword.ui.my;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.adapter.BrokerAdapter;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.bean.RecordBean;
import com.www.applyword.event.EventConstants;
import com.www.applyword.event.MessageEvent;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.utils.SPUtil;
import com.www.applyword.utils.Utils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2019/11/14 0014 17:41
 * version: 1.0
 */
public class MyEpositActivity extends BaseActivity {
    @BindView(R.id.tv_Record_Money)
    TextView tvRecordMoney;
    @BindView(R.id.rl_broker)
    RecyclerView rlBroker;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;

    private BrokerAdapter mHotJobAdapter;

    private int page = 1;
    private int Rows = 10;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.my_eposit_activity);
        setTitle("我的保证金");
        String money = getIntent().getStringExtra("money");
        tvRecordMoney.setText(money + "元");
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.getMessageKey() != null) {
            if (messageEvent.getMessageKey().equals(EventConstants.PAY) ) {
                initDate(true);
            }
        }
    }




    @Override
    protected void loadData() {
        rlBroker.setLayoutManager(new LinearLayoutManager(this));
        rlBroker.setItemAnimator(new DefaultItemAnimator());
        mHotJobAdapter = new BrokerAdapter(this);
        rlBroker.setAdapter(mHotJobAdapter);


        refresh.setOnRefreshListener(refreshLayout -> initDate(true));

        refresh.setOnLoadMoreListener(refreshLayout -> initDate(false));
        initDate(true);


    }

    private void initDate(boolean isRefresh) {
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("Page", page + "");
        params.put("Rows", Rows + "");

        params.put("UserID", SPUtil.getString(Constants.USERID));
        params.put("RecordType", "2");


        HttpClient.get(this, Constants.GETRECORDLIST, params, new CallBack<RecordBean>() {
            @Override
            public void onSuccess(RecordBean recordBean) {
                tvRecordMoney.setText(recordBean.getTotalMoney() + "元");

                if (isRefresh) {
                    refresh.finishRefresh(true);
                    if (recordBean.getRecordList().size() == 0) {
                        showEmpty(true, vsEmpty, 0);
                    } else {
                        showEmpty(false, vsEmpty, 0);
                    }
                    mHotJobAdapter.replaceAll(recordBean.getRecordList(), true);
                } else {
                    if (recordBean.getRecordList().size() > 0) {
                        refresh.finishLoadMore(true);
                        mHotJobAdapter.replaceAll(recordBean.getRecordList(), true);
                    } else {
                        refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;
            }
        });
    }

    @OnClick({R.id.tv_recharge})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_recharge:
                Utils.startActivity(RechargeActivity.class);
                break;

            default:
        }
    }
}
